import React from "react";
import { hydrateRoot } from 'react-dom/client';
// 在服务端渲染中使用 hydrateRoot，而在客户端渲染中使用 createRoot
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import App from "../views/app";
import configureStore from '../store';

const store = configureStore(window.__PRELOADED_STATE__);

delete window.__PRELOADED_STATE__;

hydrateRoot(
  document.getElementById('root'),
  <Provider store={store}>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </Provider>
);

if (process.env.NODE_ENV !== 'production' && module.hot) {
  module.hot.accept('../views/app', () => {
    const NextApp = require('../views/app').default;
    hydrateRoot(
      document.getElementById('root'),
      <Provider store={store}>
        <BrowserRouter>
          <NextApp />
        </BrowserRouter>
      </Provider>
    );
  });
}
